import { ResultType, ResultUtil } from '@xtail/result'
import { DB, PouchDBFindOptionsType } from '../classes'

/**
 * 从数据库中查询文档
 *
 * @param {DB} db 数据库实例
 * @param {PouchDBFindOptionsType} options 查询配置选项
 * @returns {Promise<ResultType>} 查询结果
 */
export const find = async (db: DB, options?: PouchDBFindOptionsType): Promise<ResultType> => {
  try {
    const res = await db.find(options)
    return ResultUtil.genSuccessResult('查询成功', res.docs)
  } catch (err: any) {
    return ResultUtil.genFailResult(err?.message ?? '查询失败', err)
  }
}

export type FindType = typeof find
